<--- %%NOBANNER%% --> labeltable.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Label a row or a column of a table in the opened word document;    |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
|Write a label for a row or a column                                 |
|  text=(text1+-text2); you can input the layout of the cell in      |
|        "text=" area; the function will read in exactly like it is; |
|  Note: you can use quoted text, but the function can only recognize|
|        the first quoted string.                                    |
|  nextdlm=F / T; Default is 'T';                                    |
|     T -- start to write to the next delimiter; e.g. next cell if   |
|          "dlm=cell"; i.e. start of the table or change to a new    |
|          format, you don't want to put delimiter before the first  |
|          cell;                                                     |
|     F -- write under the current cursor without jumping to the next|
|          delimiter;                                                |
| justify: CENTER/center -- center justified;                        |
|          LEFT/left -- left justified;                              |
|          RIGHT/right -- right justified;                           |
| fontstyle: default style is REGULAR/regular,                       |
|            default font is Times New Romans;                       |
|          ITALIC/italic -- italic;                                  |
|          UNDERLINE1/underline1 -- underline for all;               |
|          UNDERLINE2/underline2 -- underline for word only;         |
|          BOLD/bold -- boldface;                                    |
|          ARIAL/Arial --Arial;                                      |
|     NOTE: this fontstyles can be used in together to display a     |
|                 special format styles;                             |
| Color: what color do you want the text be displayed;               |
|        By default, it will keep the current color;                 |
| dlm: CELL/cell/NEXTCELL/nextcell -- put the cursor to the nextcell |
|          before executing this function;                           |
|      PARA/para/INSERTPARA/insertpara -- put the cursor to a newline|
|          before executing this function;                           |
|      COMMA/comma -- put a ', ' before execution of the currect     |
|          function;                                                 |
|      SPACE/space -- put a ' ' before execution of the currect      |
|          function;                                                 |
| leftindent: default 0;                                             |
|          often used value: 0.25;                                   |
| wordref: default is "wordsys"; word reference;                     |
| NOTE: this function is created for the entire datastep, instead of |
|       one label per iteration it must be used outside a data step; |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %labeltable(text="this is a test", nextdlm=F);            |
| Usage:   %labeltable(text=,nextdlm=T,justify=CENTER,               |
|                      fontstyle=regular, fontsize=10, color=,       |
|                      dlm=CELL,leftindent=0, wordref=wordsys);      |
\-------------------<-- End of Files Created-->---------------------*/
%macro labeltable(text=,nextdlm=T,justify=CENTER,fontstyle=regular,
                  fontsize=10, color=, dlm=CELL,leftindent=0,
                  wordref=wordsys);
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  2-27-2001 11:44pm;                |
| Modified: 7-15-2001 10:19am;                |
| Purpose:  label a table with the contents of|
|           text;                             |
\--------------------------------------------*/
%local qpairs _i_ ipairnwords numi _j_ iblock currpos localtext colorint;
%let hascolor=;
%if &color ne %then %do;
   %let hascolor =1;
   %let colorint=%colorint(&color);
%end;
%else %if (&hascolor=1)%then %do;
   %let colorint=&color_int;
%end;
%else %do;
   %let hascolor=0;
   %let colorint=0;
%end;

%let qpairs=1;
/**** Used to find out the characters among the text names *****/
%let qtext1=%qscan(&text, &qpairs, %str(''""));
%let qtext=&qtext1;
%do %while(%length(&&qtext&qpairs) gt 0);
   %let qpairs=%eval(&qpairs+1);
   %let qtext&qpairs=%qscan(&text, &qpairs, %str(''""));
   %if &&qtext&qpairs ne %then %do;
      %let qtext=&qtext &&qtext&qpairs;
   %end;
%end;
%let qpairs=%eval(&qpairs-1);
%let nwords=1;
/**** Used to find out the characters among the text names *****/
%let text1=%qscan(&qtext, &nwords, %str( (){}[]+,-/\%:><±*^°$#@~=|));
%do %while(%length(&&text&nwords) gt 0);
   %let nwords=%eval(&nwords+1);
   %let text&nwords=%qscan(&qtext, &nwords, %str( (){}[]+,-/\%:><±*^°$#@~=|));
%end;
%let nwords=%eval(&nwords-1);
%if (%index(&qtext,&text1)>1) %then %do;
   %let block0="%substr(&qtext,1,%eval(%index(&qtext,&text1)-1))";
   %let text=%substr(&qtext,%index(&qtext,&text1),%length(&qtext));
%end;
%else %do;
   %let block0="";
   %let text=&qtext;
%end;
%do _j_=1 %to &nwords;
   %let block&_j_="%qscan(&text, &_j_, %str(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.))";
%end;
/**** Used to find out the characters among the text names *****/
Data _null_;
  file &wordref lrecl=2000;
  length str $2000.;
  put '[FontSize 10]';
  %if (%quote(%upcase(&nextdlm))=T) %then %do;
     %if (%index(%upcase(&dlm),CELL)) %then %do;
        put '[NextCell]';
         %end;
     %else %if (%index(%upcase(&dlm),PARA) or %index(%upcase(&dlm),ENTER)) %then %do;
        %if (%index(%upcase(&dlm), OVERW)) %then %do;
           put '[CharRight 1]';
           put '[EndOfLine 1]';
                %end;
                %else %do;
           put '[InsertPara]';
        %end;
     %end;
     %else %if (%index(%upcase(&dlm),SPACE)) %then %do;
        put '[Insert " "]';
     %end;
     %else %if (%index(%upcase(&dlm),COMMA)) %then %do;
        put '[Insert ", "]';
     %end;
  %end;
     str='[FormatFont .Points = "'||trim(left(put(&fontsize, 3.0)))||'", .Color = '||trim(left(put(&colorint, 3.0)))||', .Strikethrough = 0, .Superscript = 0,
            .Subscript = 0, .Hidden = 0, .SmallCaps = 0, .AllCaps = 0, .Spacing = "0 pt",
            .Position = "0 pt"]';
  put str;
     str='[FormatFont  .Kerning = 0, .Bold = 0, .Italic = 0, .KerningMin = "", .Tab = "0"
            .Underline = 0, .Font = "Times New Roman"]';
  put str;
  %if (%index(%upcase(&fontstyle),BOLD)) %then %do;
      str='[FormatFont  .Bold = 1]';
  put str;
  %end;
  %if (%index(%upcase(&fontstyle),ITALIC)) %then %do;
     str='[FormatFont .Italic = 1]';
  put str;
  %end;
  %if (%index(%upcase(&fontstyle),UNDERLINE1)) %then %do;
        str='[FormatFont .Underline = 1]';
     put str;
  %end;
  %else %if (%index(%upcase(&fontstyle),UNDERLINE2)) %then %do;
        str='[FormatFont .Underline = 2]';
     put str;
  %end;
  %if (%index(%upcase(&fontstyle),ARIAL)) %then %do;
     str='[FormatFont .Font = "Arial"]';
  put str;
  %end;

  %if (%quote(%upcase(&justify))=LEFT) %then %do;
    put '[LeftPara]';
  %end;
  %else %if (%quote(%upcase(&justify))=RIGHT) %then %do;
    put '[RightPara]';
  %end;
  %else %do;
    put '[CenterPara]';
  %end;
      str= '[FormatParagraph .LeftIndent = "'||trim(left(put(&leftindent, 4.2)))||'" + Chr$(34)]';
  put str;
     str='[Insert "'||&block0||'"]';
  put str;
  %do _ij_=1 %to &nwords;
        str= '[Insert "'||trimn(left("&&text&_ij_"))||'"]'; 
     put str;
       %if (%index(&&block&_ij_,+-)) %then %do;
       put '[Insert " "]';
           str= '[InsertSymbol .Font = "Symbol", .CharNum = "177"]';
        put str;
       put '[Insert " "]';
    %end;
    %else %if (%index(&&block&_ij_,>=)) %then %do;
          put '[Insert " "]';
          str= '[InsertSymbol .Font = "Symbol", .CharNum = "179"]';
        put str;
        put '[Insert " "]';
     %end;
     %else %if (%index(&&block&_ij_,<=)) %then %do;
        put '[Insert " "]';
           str= '[InsertSymbol .Font = "Symbol", .CharNum = "163"]';
        put str;
        put '[Insert " "]';
     %end;
     %else %if (%index(&&block&_ij_,*)) %then %do;
        put '[Insert " "]';
           str= '[InsertSymbol .Font = "Symbol", .CharNum = "180"]';
        put str;
        put '[Insert " "]';
     %end;
     %else %do;
          %if (&_ij_ < &nwords) %then %do;
              str='[Insert "'||&&block&_ij_||'"]';
             put str;
        %end;
     %end;
  %end;
run;
%mend labeltable;